package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.vo.album.CategoryVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;
	@Autowired
	private CategoryMapper categoryMapper;
	@Autowired
	BaseAttributeMapper baseAttributeMapper;

//	@Override
//	public List<CategoryVo> getBaseCategoryList() {
//		//categoryMapper.getBaseCategoryList();
//		return categoryMapper.getBaseCategoryList();
//	}

	@Override
	public List<BaseCategory1> findAllCategory1() {
		List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
		return baseCategory1s;
	}

	@Override
	public CategoryVo getAllCategoryList(Long category1Id) {
		return categoryMapper.getAllCategoryList(category1Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {

		return  categoryMapper.findTopBaseCategory3(category1Id);
	}

	@Override
	@GuiGuCache(prefix = "category:")
	public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {
		return categoryMapper.getCategoryViewByCategory3Id(category3Id);
	}

	@Override
	public List<BaseAttribute> findAttributeByCategory1Id(Long category1Id) {
		return baseAttributeMapper.selectBaseAttributeList(category1Id);
	}

	@Override
	public List<CategoryVo> getBaseCategoryList() {
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		List<CategoryVo> list =new ArrayList<>();
		Map<Long, List<BaseCategoryView>> collect = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		collect.forEach(new BiConsumer<Long, List<BaseCategoryView>>() {
			@Override
			public void accept(Long aLong, List<BaseCategoryView> baseCategoryViews) {
				CategoryVo categoryVo=new CategoryVo();
				categoryVo.setCategoryId(aLong);
				categoryVo.setCategoryName(baseCategoryViews.get(0).getCategory1Name());

				List<CategoryVo> list1=new ArrayList<>();
				Map<Long, List<BaseCategoryView>> collect1 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
				collect1.forEach(new BiConsumer<Long, List<BaseCategoryView>>() {
					@Override
					public void accept(Long aLong, List<BaseCategoryView> baseCategoryViews) {
						CategoryVo categoryVo1=new CategoryVo();
						categoryVo1.setCategoryId(aLong);
						categoryVo1.setCategoryName(baseCategoryViews.get(0).getCategory2Name());
						List<CategoryVo> list2=new ArrayList<>();
						baseCategoryViews.forEach(new Consumer<BaseCategoryView>() {
							@Override
							public void accept(BaseCategoryView baseCategoryView) {
								CategoryVo categoryVo2=new CategoryVo();
								categoryVo2.setCategoryId(baseCategoryView.getCategory3Id());
								categoryVo2.setCategoryName(baseCategoryView.getCategory3Name());
								list2.add(categoryVo2);
							}
						});
						categoryVo1.setCategoryChild(list2);
						list1.add(categoryVo1);
					}
				});
				categoryVo.setCategoryChild(list1);
				list.add(categoryVo);
			}
		});
		return list;
	}
}
